
import 'package:flutter/cupertino.dart';
import 'package:furaha_mkopo/commonTool/fm_jump_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_language_tool.dart';
import 'package:furaha_mkopo/commonUI/fm_progress_hud.dart';
import 'package:furaha_mkopo/commonUI/fm_toast.dart';
import 'package:furaha_mkopo/manager/fm_device_manager.dart';
import 'package:furaha_mkopo/page/me/fm_cancel_account_api.dart';
import 'package:furaha_mkopo/page/me/fm_logout_api.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../cache/fm_cache_tool.dart';
import '../../net/fm_net_param_manager.dart';
import 'fm_me_api.dart';
import 'fm_me_model.dart';

class FMMeController extends GetxController {

  List<String> orderNames = [FMLanguageTool.getText("Repayment"),FMLanguageTool.getText("Applying"),FMLanguageTool.getText("Finished"),FMLanguageTool.getText("All")];
  List<String> orderImageNames = ["me_order_repay","me_order_applying","me_order_finish","me_order_all"];
  List<String> orderImageIconNames = ["me_order_repay_icon","me_order_applying_icon","me_order_finish_icon","me_order_all_icon"];

  List<String> functionImageNames = [
    "me_function_1","me_function_2","me_function_3","me_function_4","me_function_5","me_function_1"
  ];

  String appVersion = "";

  FMMeModel? meModel;

  final refreshController = RefreshController();

  @override
  void onInit() {
    super.onInit();
    getData();
  }

  void refreshData() {

    FMProgressHud.showLoading();

    FMMeApi api = FMMeApi();
    api.send<FMMeModel,FMMeModel>(decodeType: FMMeModel()).then((value){
      FMProgressHud.hideLoading();

      if (value.code == 0) {
        meModel = value.data;
        update();

        refreshController.refreshCompleted();
      }
    });
  }

  void getData() async {
    appVersion = await FMDeviceManager().appVersion();
  }

  String get phone {
    String p = meModel?.showed?.bold ?? "";
    return p;
  }

  bool get isIdentify {
    return meModel?.fists == 1;
  }

  String get accountCancelTitle{
    String title = meModel?.clenched?.soul ?? "";
    return title;
  }

  String get protocolTitle{
    String title = meModel?.clenched?.sentence ?? "";
    return title;
  }

  int get functionCount {
    return meModel?.beamed?.length ?? 0;
  }

  String functionImageNameAtIndex(int index) {
    List<FMMeBeamedModel>? beamed = meModel?.beamed;

    if (beamed == null) return "";
    if (index >= beamed.length) return "";

    FMMeBeamedModel model = beamed[index];

    return model.god ?? "";
  }

  String functionNameAtIndex(int index) {
    List<FMMeBeamedModel>? beamed = meModel?.beamed;

    if (beamed == null) return "";
    if (index >= beamed.length) return "";

    FMMeBeamedModel model = beamed[index];

    return model.soul ?? "";
  }

  String functionUrlAtIndex(int index) {
    List<FMMeBeamedModel>? beamed = meModel?.beamed;

    if (beamed == null) return "";
    if (index >= beamed.length) return "";

    FMMeBeamedModel model = beamed[index];

    return model.bidding ?? "";
  }

  String get identityImageName {
    bool flag = meModel?.fists == 1;
    return FMLanguageTool.getAssets("assets/me/${flag ? "me_identify" : "me_un_identify"}");
  }
}